package com.capacitydorm.server.dao;

import com.capacitydorm.server.domain.Student;
import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

@org.apache.ibatis.annotations.Mapper
public interface StudentMapper extends Mapper<Student> {
    /**
     * 分页查询
     * @return
     */
    @Select("select  *   from   c_student")
    @Results(id="student",value = {
            @Result(property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "classes",column = "classes"),
            @Result(property = "phone",column = "phone"),
            @Result(property = "state",column = "state"),
            @Result(property = "lastout",column = "lastout"),
            @Result(property = "lastin",column = "lastin"),
            @Result(property = "wxuserId",column = "wxuser_id"),
            @Result(property = "reason",column = "reason"),
            @Result(property = "wxuser",one =@One(select = "com.capacitydorm.server.dao.WxuserMapper.selectByPrimaryKey"),column = "wxuser_id"),
            @Result(property = "dormId",column = "dorm_id"),
            @Result(property = "dorm" ,one=@One(select = "com.capacitydorm.server.dao.DormMapper.findById"),column = "dorm_id")
    })
    public List<Student>   findStudents();

    /**
     * 根据ID查询学生
     * @param id
     * @return
     */
    @Select("select  *   from   c_student  where  id = #{id}")
    @ResultMap("student")
    public Student   findById(@Param("id") String  id);

    /**
     * 删除学生
     * @param id
     * @return
     */
    @Delete("delete  from   c_student where  id=#{id}")
    @ResultMap("student")
    public  void  deleteStudent(@Param("id") String id);

    /**
     * 更新学生
     * @param student
     */
    @Update("update c_student set id=#{id} ,name=#{name}, classes=#{classes},sex=#{sex},phone=#{phone},state=#{state},lastout=#{lastout},lastin=#{lastin},wxuser_id=#{wxuserId},dorm_id=#{dormId}    where id=#{id}")
    @ResultMap("student")
    public   void editStudent(Student student);

    /**
     * 添加学生
     * @param student
     */
    @Insert("insert into t_student(id,name,classes,sex,phone,state,lastout,lastin,wxuser_id,dorm_id) values (#{id},#{name},#{classes},#{sex},#{phone},#{state},#{lastout},#{lastin},#{wxuserId},#{dormId)}")
    @ResultMap("student")
    public   void     addStudent(Student  student);

    /**
     * 根据宿舍ID查询所有
     * @param id
     * @return
     */
    @Select("select  *  from  c_student  where   dorm_id=#{id}")
    @ResultMap("student")
    public  List<Student>    findByDormId(@Param("id") Integer id);

    @Select("SELECT id,classes,sex,  `name`, state,lastin,lastout  FROM  c_student  WHERE   dorm_id=#{id}")
    @ResultMap("student")
    public  List<Student>    findByDId(@Param("id") Integer id);

    /**
     * 多条件查询
     */
    @Select("<script>select  *  from  c_student  where   1=1" +
            "<if  test='id !=null '>and  id=#{id}</if>" +
            "<if  test='sex !=null '>and  sex=#{sex}</if>" +
            "<if  test='name !=null '>and  name like #{name}</if>" +
            "<if  test='classes !=null '>and  classes=#{classes}</if>" +
            "<if  test='state !=null '>and  state=#{state}</if>" +
            "<if  test='phone !=null '>and  phone=#{phone}</if>" +
            "<if  test='lastout !=null '>and  lastout=#{lastout}</if>" +
            "<if  test='lastin !=null '>and  lastin=#{lastin}</if>" +
            "<if  test='wxuserId !=null '>and  wxuser_id=#{wxuserId}</if>" +
            "<if  test='dormId !=null '>and  dorm_id=#{dormId}</if>" +
            "<if  test='reason !=null '>and  reason=#{reason}</if>" +
           "</script>")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "classes",column = "classes"),
            @Result(property = "phone",column = "phone"),
            @Result(property = "state",column = "state"),
            @Result(property = "lastout",column = "lastout"),
            @Result(property = "lastin",column = "lastin"),
            @Result(property = "wxuserId",column = "wxuser_id"),
            @Result(property = "reason",column = "reason"),
            @Result(property = "wxuser",one =@One(select = "com.capacitydorm.server.dao.WxuserMapper.findById"),column = "wxuser_id"),
            @Result(property = "dormId",column = "dorm_id"),
            @Result(property = "dorm" ,one=@One(select = "com.capacitydorm.server.dao.DormMapper.findById"),column = "dorm_id")
    })
    public List<Student>    findStudent(Student  student);

    /**
     * 根据姓名迷糊查询
     * @param name
     * @return
     */
    @Select("select  *  from    c_student  where   name   like  #{name}")
    @ResultMap("student")
    public    List<Student>    findStudentByName(@Param("name") String  name);

    /**
     * 修改全部学生状态
     */
    @Update("UPDATE c_student SET state = 0 ")
    void changeAllStudent();
}
